home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 101 / CD-ROM 101.iso / compl / maya5ple / Install_MayaPLE5_English.exe / Maya / Data1.cab / art3dPaintAssignFileTextures < prev    next >
Encoding:
Text File  |  2003-07-17  |  21.2 KB  |  733 lines

  1. // Copyright (C) 1997-2002 Alias|Wavefront,
  2. // a division of Silicon Graphics Limited.
  3. //
  4. // The information in this file is provided for the exclusive use of the
  5. // licensees of Alias|Wavefront.  Such users have the right to use, modify,
  6. // and incorporate this code into other products for purposes authorized
  7. // by the Alias|Wavefront license agreement, without fee.
  8. //
  9. // ALIAS|WAVEFRONT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  10. // INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  11. // EVENT SHALL ALIAS|WAVEFRONT BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  12. // CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  13. // DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  14. // TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. // PERFORMANCE OF THIS SOFTWARE.
  16. //
  17. // 
  18. // Assign File Textures to selected surface
  19. //
  20. proc int findShapeIndex(
  21.     string    $surfaceArray[],
  22.     string    $shapeName
  23.     
  24. )
  25. //
  26. //    Description:
  27. //        For a given shape name find and returns its index in the 
  28. //        global active surface array
  29. //
  30. {
  31.     int    $shapeIdx;
  32.     int $numbSurfaces = size( $surfaceArray );
  33.     for ( $shapeIdx = 0; $shapeIdx < $numbSurfaces; $shapeIdx++ ) {
  34.         string $longName = $surfaceArray[$shapeIdx];
  35.         if ( $longName == $shapeName ) return $shapeIdx;
  36.     }
  37.     return -1;
  38. }
  39.  
  40.  
  41. proc string[] uniqueNodeList( 
  42.     string  $nodes[]
  43. )
  44. //
  45. //    Description:
  46. //        Returns a unique list of nodes. Eliminate
  47. //        all the dublicates.
  48. //
  49. {
  50.     string    $uniqueNodes[];
  51.     int        $nodeIndx = 0;
  52.     
  53.     for ( $node in $nodes ) {
  54.         int $found = 0;    
  55.         for ($uNode in $uniqueNodes ) {
  56.             if ( $node == $uNode ) {
  57.                 $found = 1;
  58.                 break;
  59.             }
  60.         }
  61.  
  62.         // Add it to the list.
  63.         if ( $found == 0 ) { 
  64.             $uniqueNodes[$nodeIndx] = $node;
  65.             $nodeIndx ++;
  66.         }
  67.     }
  68.  
  69.     return $uniqueNodes;
  70. }
  71.  
  72.  
  73. proc string[] findConnectedShapes(
  74.     string $shadingEngine
  75. )
  76. //
  77. //     Description:
  78. //        Returns a list of shape names assigned to a given
  79. //        shading engine node
  80. //
  81. {
  82.     string     $shadingPlug       = $shadingEngine + ".dagSetMembers";
  83.     string     $tmpTransNodes[] = `listConnections -s true $shadingPlug`; 
  84.     string  $transNodes[]      = uniqueNodeList( $tmpTransNodes );
  85.  
  86.     string     $shapeNodes[];
  87.     int        $idx = 0;
  88.     for ( $trans in $transNodes ) {
  89.         string $shapes[] = `listRelatives -shapes -pa $trans`;
  90.         for ( $shape in $shapes ) {
  91.             // Skip intermediate objects.
  92.             int $intermediate = `getAttr ($shape + ".intermediateObject")`;
  93.             if ( $intermediate ) 
  94.                 continue;
  95.  
  96.             // Add it to the list.
  97.             $shapeNodes[$idx] = $shape;
  98.             $idx ++;    
  99.         }
  100.     }
  101.     return $shapeNodes;
  102. }
  103.  
  104.  
  105. proc string    getMaterialInfoNode( 
  106.     string     $messagePlug
  107. )
  108. //
  109. //    Description:
  110. //        Returns a materialInfo connected through a given message plug
  111. //
  112. {
  113.     string    $connections[] = `listConnections $messagePlug`;
  114.  
  115.     for ( $item in $connections ) {
  116.         if ( `objectType $item` == "materialInfo" ) 
  117.             return $item;
  118.     }
  119.     return "";
  120. }
  121.  
  122.  
  123. proc string [] getShadingEngines( 
  124.     string $shape 
  125. //
  126. //    Description:
  127. //        Returns a unique list of shading engines for 
  128. //        a given shape.
  129. //
  130. {
  131.     // Get all the shading engines.
  132.     string $shadingEngs[] = `listConnections -d true -t "shadingEngine" $shape`;
  133.  
  134.     // Eliminte the dublicates.
  135.     string $uniqueEngs[]  = uniqueNodeList( $shadingEngs );
  136.  
  137.     return $uniqueEngs;
  138. }
  139.  
  140.  
  141. proc string createShadingEngine(
  142.     string $shape
  143. )
  144. //
  145. //    Description:
  146. //        Creates a shading engine for a given shape. It is 
  147. //        assumed that the shape does not have a shading engine 
  148. //        assign to it. It returns the shading network 
  149. //        connections.
  150. //
  151. {
  152.        string $group      = `sets -renderable true -empty`;
  153.     string $material = `shadingNode -asShader blinn`;
  154.     string $shadingGroupName =  "blinnSG" + $group;
  155.     rename $group $shadingGroupName;
  156.  
  157.     surfaceShaderList -add $shadingGroupName $material;
  158.     sets -e -forceElement $shadingGroupName $shape;
  159.  
  160.     // Return the shading engine connections.
  161.     string $sEng[] = `listConnections -d true -t "shadingEngine" $shape`;
  162.     return $sEng[0];
  163. }
  164.  
  165.  
  166. proc string createFileTextureNode( )
  167. // 
  168. //    Description:
  169. //        Creates and returns a file texture node
  170. // 
  171. {
  172.     // Create a new texture and placement nodes.
  173.     string $texture  = `shadingNode -asTexture file`;
  174.     string $place2d  = `shadingNode -asUtility place2dTexture`;
  175.     
  176.     // Connect them together
  177.     fileTexturePlacementConnect( $texture, $place2d );
  178.  
  179.     // we need to make the connection the the filter ourselves.
  180.     connectAttr ($place2d + ".outUV") ($texture + ".uv");
  181.     connectAttr ($place2d + ".outUvFilterSize") ($texture + ".uvFilterSize");
  182.  
  183.     // Return a newly created file texture node.
  184.     return $texture;
  185. }
  186.  
  187.  
  188. proc int findShapeInSwitchNode(
  189.     string     $shape,
  190.     string    $switchNode
  191. )
  192. //
  193. //    Description:
  194. //        This function returns an index to the surface int
  195. //        the switch node if the surface is already connected
  196. //        to that switch node. Otherwise -1 is returned.
  197. //
  198. {
  199.     string    $nType = `nodeType $shape`;
  200.  
  201.     string    $switchPlug = $switchNode + ".input";
  202.     string    $shapes[];
  203.  
  204.     if ( $nType == "mesh" ) {
  205.         $shapes = `listConnections -sh 1 -type "mesh" $switchPlug`;
  206.     } else if ( $nType == "nurbsSurface" ) {
  207.         $shapes = `listConnections -sh 1 -type "nurbsSurface" $switchPlug`;
  208.     } else if (  $nType == "subdiv" ) {
  209.         $shapes = `listConnections -sh 1 -type "subdiv" $switchPlug`;
  210.     } else {
  211.         return -1;
  212.     }
  213.  
  214.     int $idx;
  215.     int $num = size( $shapes );
  216.  
  217.     for ($idx = 0; $idx < $num; $idx++ ) {
  218.         if ( $shape == $shapes[$idx] ) 
  219.             return $idx; 
  220.     }
  221.     return -1;
  222. }
  223.  
  224.  
  225. proc string createSwitchNode(
  226.     string $materialPlug    
  227. )
  228. // 
  229. // Description:
  230. //         Create a switch node and connect it to the materialPlug
  231. //
  232. {
  233.     string $switchNode;
  234.  
  235.     string $dataType = `getAttr -type $materialPlug`;
  236.     if ( $dataType == "float3") {
  237.         float $color[]     = `getAttr $materialPlug`;
  238.         $switchNode = `shadingNode -asUtility tripleShadingSwitch`;
  239.         setAttr ($switchNode + ".default") $color[0] $color[1] $color[2];
  240.     } 
  241.     else if ( $dataType == "float" ) {
  242.         float $color = `getAttr $materialPlug`;
  243.         $switchNode  = `shadingNode -asUtility singleShadingSwitch`;
  244.         setAttr ($switchNode + ".default") $color;
  245.     }
  246.  
  247.     // Connect a newly created switch node to the material attribute.
  248.     connectAttr -f ($switchNode + ".output") $materialPlug;
  249.     return $switchNode;
  250. }
  251.  
  252.  
  253. proc createMultiFileTexture( 
  254.     string     $switchShapes[], 
  255.     string    $shadingEng, 
  256.     string    $materialAttr
  257. )
  258. //
  259. //    Description:
  260. //        Creates a switch node and file textures for all shapes
  261. //        int switchShapes array for the specified attributes and 
  262. //        connectes them properly. 
  263. //
  264. {
  265.     string  $materialConn[] = `listConnections ($shadingEng + ".surfaceShader")`;
  266.     string    $material         = $materialConn[0];
  267.     string  $materialPlug     = $material + "." + $materialAttr;
  268.     string  $shaderSG[];
  269.  
  270.     if ( "normalCamera" == $materialAttr ) {     
  271.         string    $textures[]     = `listConnections $materialPlug`;
  272.         string     $bumpNode;
  273.         if ( size( $textures ) > 0 ) {
  274.             string     $node = $textures[0];
  275.             string     $nType = `nodeType $node`;
  276.  
  277.             if ( $nType == "bump2d" ) {
  278.                 $bumpNode = $textures[0];
  279.             } else {
  280.                 $bumpNode = `shadingNode -asUtility bump2d`;
  281.                    connectAttr -f ($bumpNode + ".outNormal") $materialPlug;
  282.             }
  283.         } 
  284.         else {
  285.             // This is a bump map, so first create bump2d utility node
  286.             // and connect it to the shader and the texture.
  287.             $bumpNode = `shadingNode -asUtility bump2d`;
  288.                connectAttr -f ($bumpNode + ".outNormal") $materialPlug;
  289.         }
  290.  
  291.         // Reassign a material plug.
  292.         $materialPlug = $bumpNode + ".bumpValue";
  293.     }
  294.     else if ( "displacement" == $materialAttr ) {
  295.         if( $material == "lambert1" )
  296.         {
  297.             $shaderSG = `listConnections ($material + ".outColor")`;
  298.             if( size( $shaderSG ) == 2 )
  299.             {
  300.                 $materialPlug = ($shaderSG[1] + ".displacementShader");
  301.             }
  302.             else if( size( $shaderSG ) == 1 ) // new
  303.             {
  304.                 $materialPlug = ($shaderSG[0] + ".displacementShader"); // new
  305.             }
  306.             else
  307.             {                
  308.                 $msg =  "Cannot paint with " + $material + ", shader has multiple shader groups";
  309.                 warning $msg;
  310.             }
  311.         }
  312.         else
  313.         {
  314.             $shaderSG = `listConnections ($material + ".outColor")`;
  315.             // Note:  If size of ShaderGroup is zero, new Shader Group is created
  316.             if( size( $shaderSG ) == 1 )
  317.             {
  318.                 $materialPlug = ($shaderSG[0] + ".displacementShader");
  319.             }
  320.             else if( size( $shaderSG ) > 1 )
  321.             {    
  322.                 // There are multiple shading group connected with same attribute.
  323.                 $msg =  "Cannot paint with " + $material + ", shader has multiple shader groups";
  324.                 warning $msg;
  325.                 return;
  326.             }
  327.         }
  328.         string    $textures[]     = `listConnections $materialPlug`;
  329.                         
  330.         string     $dispNode;
  331.  
  332.         if ( size( $textures ) > 0 ) {
  333.             string     $node = $textures[0];
  334.             string     $nType = `nodeType $node`;
  335.  
  336.             if ( $nType == "displacementShader" ) {
  337.                 $dispNode = $textures[0];
  338.             } else {
  339.                 $dispNode = `shadingNode -asUtility displacementShader`;                   
  340.                 connectAttr -f ($dispNode + ".displacement") $materialPlug;
  341.             }
  342.         } 
  343.         else {        
  344.             // This is a displacement map, so first create displacement Shader utility node
  345.             // and connect it to the shader and the texture.
  346.             $dispNode = `shadingNode -asUtility displacementShader`;               
  347.             connectAttr -f ($dispNode + ".displacement") $materialPlug;
  348.         }
  349.  
  350.         // Reassign a material plug.
  351.         $materialPlug = $dispNode + ".displacement";
  352.     }
  353.  
  354.     // Proceede only if either nothing is connected to the specified 
  355.     // material attribute or there is already a switch node.
  356.     string  $fileTextureName = "";
  357.     string    $existingTexture;
  358.     string    $textureAttr;
  359.     string     $switchNodeAttr;
  360.     string    $switchNode; 
  361.  
  362.     string    $textures[]     = `listConnections -d off -s on $materialPlug`;    
  363.     if ( size( $textures ) > 0 ) {
  364.         // There is already a node connected to this attribute
  365.         // - check if this is a switch node and if not, return.
  366.         string $node  = $textures[0];
  367.         string $nType = `nodeType $node`;
  368.         if ( $nType == "file" ) {
  369.             // Store the file texture name.
  370.             string $fileTextureNamePlug = $textures[0] + ".fileTextureName";
  371.             $fileTextureName = `getAttr $fileTextureNamePlug`;
  372.  
  373.             // Create a switch node and connected to the material node.
  374.             $switchNode = createSwitchNode( $materialPlug );
  375.             $existingTexture = $textures[0];
  376.         } 
  377.         else {
  378.             // We assume here that this is a switch node.            
  379.             $switchNode = $textures[0];
  380.         }
  381.     } 
  382.     else {
  383.         $switchNode    = createSwitchNode( $materialPlug );
  384.     }
  385.  
  386.  
  387.     // Get the names of the attributes for the switch node 
  388.     // and the file texture node.
  389.     string $nType = `nodeType $switchNode`;
  390.     if ( $nType == "tripleShadingSwitch" ) {
  391.         $switchNodeAttr = "inTriple";
  392.         $textureAttr    = "outColor";
  393.     } 
  394.     else if ( $nType == "singleShadingSwitch" ) { 
  395.         $switchNodeAttr = "inSingle";
  396.         $textureAttr    = "outAlpha";
  397.     } 
  398.     else { 
  399.         // This is neither a switch nor a file texture node so return.
  400.         warning("No switch node or file node connected to the paint attribute"); 
  401.         return;
  402.     }
  403.  
  404.     // Find out how many surfaces are connected to the switch node.
  405.     string    $tmpSwitchPlug = $switchNode + ".input";
  406.     string  $tmpSwitchConn[] = `listConnections $tmpSwitchPlug`;
  407.     int $nextMultiIndex = size( $tmpSwitchConn )/2; 
  408.  
  409.     // Connect the first surface to the existing texture.
  410.     int    $startIdx = 0;
  411.     if ( $existingTexture != "" ) {
  412.         string    $shape = $switchShapes[0];  
  413.         string    $shapePlug = $shape + ".instObjGroups[0]"; 
  414.         int     $srfIdx = findShapeInSwitchNode( $shape, $switchNode ); 
  415.         if ( $srfIdx == -1 ) { 
  416.             string $switchNodeSurfacePlug = 
  417.                 $switchNode+".input[" +$nextMultiIndex +"].inShape";
  418.             string $switchNodeTexturePlug = 
  419.                 $switchNode + ".input[" + $nextMultiIndex + "]." + $switchNodeAttr;
  420.  
  421.             string $texture    = $existingTexture;
  422.             string $texturePlug = $texture + "." + $textureAttr;
  423.  
  424.             // Connect shape and texture to the switch node.
  425.             connectAttr -f $shapePlug   $switchNodeSurfacePlug;
  426.             connectAttr -f $texturePlug $switchNodeTexturePlug;
  427.  
  428.             // Increase the indices.
  429.             $startIdx ++;
  430.             $nextMultiIndex ++;
  431.         }
  432.     }
  433.  
  434.     // Now finally, create file textures for each of the 
  435.     // surfaces and assign pairs (shape+texture) to the 
  436.     // switch node.
  437.     int    $idx;
  438.     int    $switchShapeSize = size( $switchShapes );
  439.     for ($idx = $startIdx; $idx<$switchShapeSize; $idx++ ) {
  440.         // Get the shape  and its plug.
  441.         string $shape = $switchShapes[$idx];  
  442.         string $shapePlug = $shape + ".instObjGroups[0]"; 
  443.  
  444.         // We need to check if this surface is already 
  445.         // connected to the switch node.
  446.         int $srfIdx = findShapeInSwitchNode( $shape, $switchNode ); 
  447.         if ( $srfIdx != -1 ) 
  448.             continue;
  449.  
  450.         // Get the switch node plugs.
  451.         string $switchNodeSurfacePlug = 
  452.             $switchNode+".input[" + $nextMultiIndex +"].inShape";
  453.         string $switchNodeTexturePlug = 
  454.             $switchNode + ".input[" + $nextMultiIndex + "]." + $switchNodeAttr;
  455.  
  456.         // Create a file texture node and get its plug.
  457.         string $texture     = createFileTextureNode();
  458.         string $texturePlug = $texture + "." + $textureAttr;
  459.  
  460.         // If the file texture name is not empty, then set 
  461.         // the name of the file texture to that name.
  462.         if ( $fileTextureName != "" ) {
  463.             setAttr -type "string" ($texture + ".fileTextureName") $fileTextureName;
  464.         } 
  465.  
  466.         // Connect shape and texture to the switch node.
  467.         connectAttr -f $shapePlug   $switchNodeSurfacePlug;
  468.         connectAttr -f $texturePlug $switchNodeTexturePlug;
  469.  
  470.         // Increase the indices.
  471.         $nextMultiIndex ++;
  472.     }
  473. }
  474.  
  475.  
  476. proc createSingleFileTexture( 
  477.     string     $shape, 
  478.     string    $shadingEng, 
  479.     string    $materialAttr
  480. )
  481. //
  482. //    Description:
  483. //        Creates a file texture node for a given shape and connects it 
  484. //        to the specified attribute on the material (shader) node
  485. //
  486. {
  487.     // Get the material node
  488.     string    $textureAttr    = "";
  489.     string  $materialConn[] = `listConnections ($shadingEng + ".surfaceShader")`;
  490.     string    $material         = $materialConn[0];
  491.  
  492.     string $msg =  $shadingEng + " cannot paint the current attribute because there is a non-file texture connected.";
  493.  
  494.     // Proceede only if nothing is no file texture node connected.
  495.  
  496.     string $textures[];
  497.     string $dispShadingGroupShader;
  498.     string $shaderSG[];
  499.     
  500.     if( $materialAttr == "displacement" )
  501.     {
  502.         if( $material == "lambert1" )
  503.         {
  504.             $shaderSG = `listConnections ($material + ".outColor")`;
  505.             if( size( $shaderSG ) == 2 )
  506.             {
  507.                 $dispShadingGroupShader = ($shaderSG[1] + ".displacementShader");
  508.             }
  509.             else if( size( $shaderSG ) == 1 )
  510.             {
  511.                 $dispShadingGroupShader = ($shaderSG[0] + ".displacementShader");
  512.             }
  513.             else
  514.             {                
  515.                 $msg =  " Cannot paint with " + $material + ", shader has multiple shader groups";
  516.                 warning $msg;
  517.                 return;
  518.             }
  519.         }
  520.         else
  521.         {
  522.             $shaderSG = `listConnections ($material + ".outColor")`;
  523.             // Note:  If size of ShaderGroup is zero, new Shader Group is created
  524.             if( size( $shaderSG ) == 1 )
  525.             {                
  526.                 $dispShadingGroupShader = ($shaderSG[0] + ".displacementShader");
  527.             }
  528.             else if( size( $shaderSG ) > 1 )
  529.             {
  530.                 // There are multiple shading group connected with same attribute.
  531.                 $msg =  " Cannot paint with " + $material + ", shader has multiple shader groups";
  532.                 warning $msg;
  533.                 return;
  534.             }
  535.         }
  536.         $textures = `listConnections ($dispShadingGroupShader)`;
  537.     }
  538.     else
  539.     {
  540.         $textures = `listConnections ($material + "." + $materialAttr)`;
  541.     }
  542.     if ( "normalCamera" == $materialAttr ) { 
  543.         string  $bumpNode = "";
  544.         if ( size( $textures ) > 0 ) {
  545.             string $nType = `nodeType $textures[0]`;
  546.             if ( $nType == "bump2d" ) {
  547.                 // There is already a bymp node connected.
  548.                 $bumpNode = $textures[0];
  549.  
  550.                 // Check if there is also a file texture node there.
  551.                 string    $bumpConns[] = `listConnections ($bumpNode + ".bumpValue")`;
  552.                 if ( size( $bumpConns ) > 0 ) {
  553.                     $nType = `nodeType $bumpConns[0]`;
  554.                     if ( $nType != "file" ) {
  555.                         // Print a warning if there is anything else connected.
  556.                         warning $msg;
  557.                     }
  558.                     return;
  559.                 }
  560.             }
  561.             else {
  562.                 // There only node allowed here is bump node.
  563.                 $msg =  $shadingEng + " cannot paint the current attribute because there is a no bump node connected.";
  564.                 warning $msg;
  565.                 return;
  566.             }
  567.         } 
  568.  
  569.         // If there is not bump node, create it and 
  570.         // connect it to the shader.
  571.         if ( $bumpNode == "" ) { 
  572.             $bumpNode = `shadingNode -asUtility bump2d`;
  573.                connectAttr ($bumpNode + ".outNormal") ($material + "." + $materialAttr);
  574.         }
  575.  
  576.         // Finally, create a file texture node and connect it to the bump node.
  577.         string $texture = createFileTextureNode();
  578.            connectAttr ($texture + ".outAlpha") ($bumpNode + ".bumpValue");
  579.     }
  580.     else if ( "displacement" == $materialAttr ) {
  581.         string  $dispNode = "";
  582.         if ( size( $textures ) > 0 ) {
  583.             string $nType = `nodeType $textures[0]`;
  584.             
  585.             if ( $nType == "displacementShader" ) {            
  586.                 // There is already a disp node connected.
  587.                 $dispNode = $textures[0];
  588.  
  589.                 // Check if there is also a file texture node there.
  590.                 string    $dispConns[] = `listConnections ($dispNode + ".displacement")`;
  591.                 if ( size( $dispConns ) > 0 ) {                
  592.                     $nType = `nodeType $dispConns[0]`;
  593.                     if ( $nType != "file" ) {
  594.                         // Print a warning if there is anything else connected.
  595.                         warning $msg;
  596.                     }
  597.                     return;
  598.                 }
  599.             }
  600.             else {
  601.                 // There only node allowed here is disp node.            
  602.                 $msg =  $shadingEng + " cannot paint the current attribute because there is a no displacement node connected.";
  603.                 warning $msg;
  604.                 return;
  605.             }
  606.         } 
  607.  
  608.         // If there is not disp node, create it and 
  609.         // connect it to the shader.
  610.         if ( $dispNode == "" ) {            
  611.             $dispNode = `shadingNode -asUtility displacementShader`;            
  612.             connectAttr ($dispNode + ".displacement") ($dispShadingGroupShader);
  613.         }
  614.  
  615.         // Finally, create a file texture node and connect it to the bump node.
  616.         string $texture = createFileTextureNode();
  617.            connectAttr ($texture + ".outAlpha") ($dispNode + ".displacement");
  618.     }
  619.     else {
  620.             
  621.         // Check if there is already a node connected to the attribute.
  622.         if ( size( $textures ) > 0 ) {
  623.             string $nType = `nodeType $textures[0]`;
  624.             if ( $nType != "file" ) {
  625.                 // Print a warning if there is no file texture node connected.
  626.                 warning $msg;
  627.             }
  628.             return;
  629.         }
  630.  
  631.         // Create a file texture node.
  632.         string $texture = createFileTextureNode();
  633.  
  634.         // Get the texture attribute name (plug).
  635.         string  $materialPlug = $material + "." + $materialAttr;
  636.         string    $dataType = `getAttr -type $materialPlug`;
  637.  
  638.         if ( $dataType == "float3") {
  639.             $textureAttr = "outColor";
  640.         } else if ( $dataType == "float" ) {
  641.             $textureAttr = "outAlpha";
  642.         }
  643.  
  644.         // Make the specified by the user connection.
  645.            connectAttr ($texture + "." + $textureAttr) ($material + "." + $materialAttr);
  646.     }
  647. }
  648.  
  649.  
  650. global proc art3dPaintAssignFileTextures(
  651.     string     $materialAttr
  652. //
  653. //    Description:
  654. //        Traverse the selection list, and if necessary create file textures 
  655. //        for all surfaces which are on the list. The user specifies a material
  656. //        node attribute to which the connection with the file texture needs to
  657. //        exist. If the file texture on this attribute already exists, nothing
  658. //        is done, otherwise a file texture node with a dummy texture is created
  659. //      and connected to the shape node. Paint tool has to allocate a physical
  660. //        image for the file texture and change the name into a proper one.
  661. //
  662. {
  663.     //     Get the active surfaces from the paint color context
  664.     //    and initialize the global tables.
  665.     string    $activeSrfArray[];
  666.     string    $srfNames = eval("art3dPaintCtx -q -shapenames `currentCtx`");
  667.     if ( size( $srfNames ) == 0 ) 
  668.         return;
  669.  
  670.     int        $numbActiveSrf = `tokenize $srfNames $activeSrfArray`;
  671.     int        $surfaceHasTexture[];    
  672.     for ( $idx = 0; $idx < $numbActiveSrf; $idx++ ) {
  673.         $surfaceHasTexture[$idx] = 0;    
  674.     }
  675.  
  676.     // Now generate file textures.
  677.     for ( $idx = 0; $idx < $numbActiveSrf; $idx++ ) {
  678.  
  679.         // Get the surface.
  680.         string $shape = $activeSrfArray[$idx];
  681.         if ( $shape == "" ) continue; 
  682.         if ( $surfaceHasTexture[$idx] == 1 ) continue; 
  683.  
  684.         // Find the shading engine ( if it does not exist, create one ).
  685.         string $sEng[]   = getShadingEngines( $shape );
  686.         int    $sEngSize = size( $sEng );
  687.  
  688.         if ( $sEngSize == 0 ) {
  689.             $sEng[0]  = createShadingEngine( $shape );
  690.             $sEngSize = 1;
  691.         }
  692.  
  693.         for ( $sIdx = 0; $sIdx < $sEngSize; $sIdx++ ) {
  694.             // Get the shading engine.
  695.             string $shadingEng = $sEng[$sIdx];
  696.  
  697.             // Find out how many surfaces are connected to this shading engine
  698.             // and if there is more than one, then we need a switch node.
  699.             string     $tmpShapes[] = findConnectedShapes( $shadingEng );
  700.             int        $tmpNumShapes = size( $tmpShapes );
  701.  
  702.             if ( $tmpNumShapes > 1 ) {
  703.                 // Eliminate all the shapes which are not selected.
  704.                 string  $switchShapes[];
  705.                 int     $ii = 0;
  706.                 for ( $shapeName in $tmpShapes ) {
  707.                     int $shapeIdx = findShapeIndex( $activeSrfArray, $shapeName );
  708.                     if ( $shapeIdx == -1 ) continue;
  709.                 
  710.                     // Add surface to the list.
  711.                     $switchShapes[$ii] = $shapeName;
  712.  
  713.                     // Mark the surface so we don't try to create 
  714.                     // a texture node again for it.
  715.                     $surfaceHasTexture[$shapeIdx] = 1;
  716.                     $ii ++;
  717.                 }
  718.  
  719.                 // Create a switch node.
  720.                 createMultiFileTexture( $switchShapes, $shadingEng, $materialAttr );
  721.             } 
  722.             else {
  723.                 // Create a single texture.
  724.                 createSingleFileTexture( $shape, $shadingEng, $materialAttr );
  725.  
  726.                 // Mark the surface.
  727.                 $surfaceHasTexture[$idx] = 1;
  728.             }
  729.         }
  730.     }
  731. }